//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.TestUtilities;

namespace Microsoft.Practices.Mobile.DisconnectedAgent.Tests
{
    [TestClass]
    public class CallParametersSerializerFixture
    {
        [TestMethod]
        public void SerializingNullArrayReturnsEmptyString()
        {
            string xml = CallParametersSerializer.Serialize(null);
            Assert.AreEqual(String.Empty, xml);
        }

        [TestMethod]
        public void SerializingEmptyArrayReturnsEmptyString()
        {
            string xml = CallParametersSerializer.Serialize(new object[0]);
            Assert.AreEqual(String.Empty, xml);
        }

        [TestMethod]
        public void DeserializingEmptyStringReturnsEmptyArray()
        {
            object[] parameters = CallParametersSerializer.Deserialize(String.Empty);
            Assert.IsNotNull(parameters);
            Assert.AreEqual(0, parameters.Length);
        }

        [TestMethod]
        public void DeserializingNullStringThrows()
        {
            ExtendedAssert.Throws<ArgumentNullException>(
                        delegate { object[] parameters = CallParametersSerializer.Deserialize(null); });
        }

        [TestMethod]
        public void DeserializingSerializedStringRegeneratesSingleInt()
        {
            object[] parameters = new object[] { 123 };
            string xml = CallParametersSerializer.Serialize(parameters);
            object[] parameters2 = CallParametersSerializer.Deserialize(xml);
            Assert.IsNotNull(parameters2);
            Assert.AreEqual(1, parameters2.Length);
            Assert.AreEqual(123, parameters2[0]);
            Assert.IsInstanceOfType(parameters2[0], typeof(int));
        }

        [TestMethod]
        public void SerializingWillNotStoreVersionNumber()
        {
            object[] parameters = new object[] { 888, new TestClass("teststring", 4321) };
            string xml = CallParametersSerializer.Serialize(parameters);

            Assert.IsFalse(xml.Contains("Version"), "Xml contains version number");

            object[] parameters2 = CallParametersSerializer.Deserialize(xml);
            Assert.IsNotNull(parameters2);
            Assert.AreEqual(2, parameters2.Length);
            Assert.AreEqual(888, parameters2[0]);
            Assert.IsInstanceOfType(parameters2[0], typeof(int));
            Assert.IsInstanceOfType(parameters2[1], typeof(TestClass));
            Assert.AreEqual(4321, ((TestClass)parameters2[1]).Number);
            Assert.AreEqual("teststring", ((TestClass)parameters2[1]).Text);
        }
    }

    public class TestClass
    {
        private int number;

        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public TestClass()
        {
        }

        public TestClass(string text, int number)
        {
            this.text = text;
            this.number = number;
        }
    }
}
